--
-- Author: mm
-- Date: 2018-09-22 10:59:30
--
-- CorepropView
--

local fileName = string.getModeName(...)
local CorePropView = class(fileName, app.mvc.ViewBase)

local PropNameBtnRender = import(".renders.PropNameBtnRender")
local PropDescRender = import(".renders.PropDescRender")
local CorePropUIHelp = require("app.coreProp.CorePropUIHelp")
local RadarWidget = require("app.widget.RadarWidget")
local CorePropItemTipsPanel = require("app.newFloatTips.CorePropItemTipsPanel")

CorePropView.VIEW_ROOT_RES = Res.CorePropViewCsb
function CorePropView:init()
	self._ui["Panel/Button_close"]:addClickEventListener(handler(self, self.closeView))

	self._uiBtns = {}
	local orderArr = CorePropUIHelp.getPropOrderByClass()
	for k , v in ipairs(orderArr) do
		self._uiBtns[v] = PropNameBtnRender.createWithType(v, self._ui["Panel/Panel_left/Panel_top/data_"..k])
	end
	self._curSelectType = orderArr[1]
	self._curOrder = orderArr
	for k, v in pairs(self._uiBtns) do
		v:onBtnClick(handler(self, self._onSelectPorp))
	end

	self._uiLeftPoint = self._ui["Panel/Panel_left/tlt"]

	self._uiLeftPoint2 = self._ui["Panel/Panel_point/num"]
	--右侧
	
	self._ui["Panel/Panel_left/Panel_top/Button"]:setVisible(false)
	self._ui["Panel/Panel_right/Panel_top/btn_tips"]:addClickEventListener(handler(self, self._onClickRightTips))
	self._uiRightTitle = self._ui["Panel/Panel_right/Panel_top/tlt"]
	self._uiBtnAdd = self._ui["Panel/Panel_right/btn_add"]

	self._uiPorpDesc1 = PropDescRender.create(self._ui["Panel/Panel_right/data_1"])
	self._uiPorpDesc2 = PropDescRender.create(self._ui["Panel/Panel_right/data_2"])
	self._uiPorpDesc1:setTitle(L("CorePorpDescTitle2"))
	self._uiPorpDesc2:setTitle(L("CorePorpDescTitle3"))
	self._uiPorpDesc2:setAlpha()

	self._uiBtnAdd:addClickEventListener(handler(self, self._onClickAddLevel))


	self._uiCanNotAdd = self._ui["Panel/Panel_right/Panel_lock"]
	self._uiCanNotAddDesc1 = self._ui["Panel/Panel_right/Panel_lock/text_1"]

	self._uiMaxLevelDesc = self._ui["Panel/Panel_right/Panel_max"]

	self._ui["Panel/Panel_right/Panel_lock/text_2"]:setString(string.format(L("CorePropCanNotAddLevelDesc1"), CorePropUIHelp.getMaxLevelDiff()))

	self._radarWidget = RadarWidget.new(self._ui["Panel/Panel_left/Panel_top/bg/radarNode"], 120, 5, 0.3, 0.2)

	
	display.uiAddClick(self._ui["Panel/Panel_point"], function ()
		executeFunc(self._onOperate, "openExchangePointView")
	end)
	self._ui["Panel/Panel_point/Button_plus"]:addClickEventListener(function()
		executeFunc(self._onOperate, "openExchangePointView")
	end)
	
	--全屏适配
	self._ui["Panel/tipBg"]:setVisible(false)
	-- local p = self._ui["Panel"]:convertToNodeSpace(cc.p(0, 0))
	-- self._ui["Panel/tipBg"]:setPosition(p)
	-- self._ui["Panel/tipBg"]:setContentSize(display.safe_size)
	-- self._ui["Panel/tipBg"]:setTouchEnabled(true)
	-- self._ui["Panel/tipBg"]:setSwallowTouches(false)
	
	-- display.uiAddClick(self._ui["Panel/tipBg"], function()
	-- 	self:cleanTip()
	-- end)
	-- self:onUpdate(handler(self, self._onUpdate))
end

function CorePropView:_onSelectPorp(tp)
	self._curSelectType = tp
	self:refresh()
end


function CorePropView:_onClickRightTips()
	local param = {}
	param.tipsType = Enums.TipsType.desc
	param.title = L("CorePropTipsTitle")
	param.content = CorePropUIHelp.getTipsContent(self._curSelectType)
	app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=param})
end


function CorePropView:_onClickAddLevel()
	if not self._viewData then
		return
	end
	if self._viewData.leftPoint <= 0 then
		display.pushToast(L("CorePropPointNotEnough"))
		return
	end
	
	local dialog = display.newConfirmDialog(string.format(L("CorePorpAddLevelTip"), CorePropUIHelp.getTextByType(self._curSelectType)), function()
		executeFunc(self._onOperate, "addLevel", self._curSelectType)
	end)
	UIHandler:addPopDialog(dialog, 1, true)
end


function CorePropView:setViewData(viewData)
	self._viewData = viewData
	self:refresh()
end

function CorePropView:refresh()
	self:refreshStaticUI()
end

function CorePropView:refreshStaticUI()
	if not self._viewData then
		return
	end
	local d = self._viewData
	

	-- 数据来源2个部分
	local mountConfig = CorePropUIHelp.getCurMountStarConfig()
	local totalValue = {}
	for k, v in pairs(d.values) do
		totalValue[k] = v + (mountConfig["addType"..k] or 0)
	end

	local curSelectLevel =  totalValue[self._curSelectType] 

	self._uiPorpDesc1:setData({type= self._curSelectType, level = curSelectLevel})
	self._uiPorpDesc2:setData({type= self._curSelectType, level = CorePropUIHelp.getNextStageLevel(curSelectLevel)})

	local canAdd, canNotAddLevelMap, canNotAddDesc, isMaxLevel = CorePropUIHelp.checkCanAddLevel(self._curSelectType, curSelectLevel)
	local hasPoint = UD.coreProp:getLeftPoint() > 0
	for k ,v in pairs(self._uiBtns) do
		v:setData({value = d.values[k], canNotAdd = canNotAddLevelMap[k], mountAdd = mountConfig["addType"..k]})
		v:setSelectedByType(self._curSelectType)
		if k == self._curSelectType then
			v:refreshRedPoint(canAdd and hasPoint)
		else
			local otherCanAdd = CorePropUIHelp.checkCanAddLevel(k, totalValue[k])
			v:refreshRedPoint(otherCanAdd and hasPoint)
		end
	end

	self._uiRightTitle:setString(CorePropUIHelp.getTextByType(self._curSelectType))

	if isMaxLevel then
		self._uiMaxLevelDesc:setVisible(true)
		self._uiBtnAdd:setVisible(false)
		self._uiCanNotAdd:setVisible(false)
	else
		self._uiMaxLevelDesc:setVisible(false)
		self._uiBtnAdd:setVisible(canAdd)
		self._uiCanNotAdd:setVisible(not canAdd)
		if not canAdd then
			self._uiCanNotAddDesc1:setString(canNotAddDesc)
		end
	end
	self._radarWidget:setData(CorePropUIHelp.getRadarData(totalValue, self._curOrder))
	self._uiLeftPoint:setString(string.format("%s%d", L("CoreProp_LeftPoint"), d.leftPoint))
	self._uiLeftPoint2:setString(d.leftPoint)
	display.setNodesMiddle(self._ui["Panel/Panel_left"], nil, self._uiLeftPoint, self._ui["Panel/Panel_left/Image"])
	self:refreshRedPoint()
end

function CorePropView:refreshRedPoint()
	local canExchange = UD.redPoint:getStateByKey(RedPointConstant.CorePorp_Exchange)
	self._ui["Panel/Panel_point/Button_plus/tishi"]:setVisible(canExchange)
end

function CorePropView:checkGuide()
    local params = {
        scene = Constant.FUNC_TYPE_CORE_PROP,
        sceneSelf = self,
        getFunc = self.getGuideNodeFunc,
        nextFunc = self.checkGuide
    }
    EM:notify("checkGuide", params)
end

-- 获取引导node
function CorePropView:getGuideNodeFunc(id)

end


-- function CorePropView:cleanTip()
-- 	if self._itemTip then
-- 		self._itemTip:hide()
-- 	end
-- 	self._ui["Panel/tipBg"]:setVisible(false)
-- end


-- function CorePropView:showCorePorpItemTip()
-- 	if not self._itemTip then
-- 		self._itemTip = CorePropItemTipsPanel.create()
-- 		self._itemTip:onOperate(function()
-- 			self._ui["Panel/tipBg"]:setVisible(false)
-- 		end)
-- 		self._ui["Panel/itemTipParent"]:addChild(self._itemTip)
-- 		self._ui["Panel/tipBg"]:setVisible(true)
-- 	else
-- 		self._itemTip:setVisible(true)
-- 		self._ui["Panel/tipBg"]:setVisible(true)
-- 	end
-- end


	
return CorePropView
